<?php

class DifferenceLogicComponent extends Object
{
	/**
	 *
	 * Save post
	 * @param array $data
	 * 			$data[ContentAuthor]
	 * 			$data[ContentPost]
	 */
	public function saveGame($data) {
		Utils::loadModels($this, array('Difference.DifferenceGame','Difference.DifferencePlayer'));
		// try to get the author
		$author = $this->DifferencePlayer->findByEmail($data['DifferencePlayer']['email']);
		if(!empty($author)){
			$data['DifferencePlayer'] = $author['DifferencePlayer'];
		} else {
			$result = $this->DifferencePlayer->saveIt($data, 'email');
			if($result['status']){
				return $result;
			}
		}
		$data['DifferenceGame']['different_points'] = json_encode($data['DifferenceGame']['different_points']);
		$result = $this->DifferenceGame->saveIt($data);
		return $result;
	}

	public function viewGame($hash){
		Utils::loadModels($this, array('Difference.DifferenceGame','Difference.DifferencePlayer'));
		$game = $this->DifferenceGame->findBySeoMd5Raw($hash);
		$game['DifferenceGame']['number_of_differences'] = isset($game['DifferenceGame']['different_points']) ? count(json_decode($game['DifferenceGame']['different_points'])) : 0;
		return $game;
	}

	public function syncPlayer($data){
		Utils::loadModels($this, array('Difference.DifferencePlayer'));
		$player = $this->DifferencePlayer->findByEmail($data['DifferencePlayer']['email']);
		if(empty($player)){
			// try to create him
			$this->DifferencePlayer->saveIt($data, 'email');
			$player = $this->DifferencePlayer->findByEmail($data['DifferencePlayer']['email']);
		}
		return $player;
	}

	public function randNotPlayedGame($data){
		// return a game which is not played
		Utils::loadModels($this, array('Difference.DifferenceGame','Difference.DifferenceScore'));

		// conditions
		$gameConds = array(
			'DifferenceGame.status' => DifferenceGame::STATUS_PUBLIC,
		);
		$playedConds = array(
			'player_id' => $data['DifferencePlayer']['id'],
			'resolved <>' => null
		);
		// count number of unplayed game
		$numGames = $this->DifferenceGame->find('count' , array(
			'conditions' => $gameConds,
			'fields' => 'DISTINCT id'
		));
		$numPlayed = $this->DifferenceScore->find('count', array(
			'conditions' => $playedConds,
			'fields' => 'DISTINCT game_id'
		));
		$unPlayed = $numGames - $numPlayed;
		if($unPlayed){
			$randPos = rand(0, $unPlayed - 1);
			$this->DifferenceGame->bindModels(array('hasOneDifferenceScore'), false);
			$games = $this->DifferenceGame->find('all', array(
				'fields' => array(
					'DISTINCT DifferenceScore.game_id', 'DifferenceGame.*', 'DifferenceScore.*'
				),
				'conditions' => array(
					'DifferenceGame.status' => DifferenceGame::STATUS_PUBLIC,
					'OR' => array(
						'DifferenceScore.player_id' => null,
						'DifferenceScore.player_id <>' => $data['DifferencePlayer']['id']
					)
				),
				'limit' => 1,
				'offset' => $randPos,
			));
		} else {
			$randPos = rand(0, $numGames - 1);
			$games = $this->DifferenceGame->find('all', array(
				'conditions' => array(
					'DifferenceGame.status' => DifferenceGame::STATUS_PUBLIC,
				),
				'limit' => 1,
				'offset' => $randPos
			));
		}
		return empty($games) ? null : $games[0];
	}
}